package Storage.StorageUnitTest;

import static org.junit.Assert.*;

import java.text.ParseException;

import org.junit.Before;
import org.junit.Test;

import Object.DSException;
import Storage.DSDataFileHandler;
import Task.DSTask;
import Task.DSTaskList;

//@author A0103535R
/******************** Abstract Class DSDataFileHandlerTest *******************/
public class DSDataFileHandlerTest {

	@Before
	public void setUp() throws Exception {
	}

	@Test
	public void testGetInstance() throws DSException {
		DSDataFileHandler fileHandler = DSDataFileHandler.getInstance();
		assertTrue(fileHandler != null);
	}

	@Test
	public void testSaveToDataFile() throws ParseException, DSException {
		DSDataFileHandler fileHandler = DSDataFileHandler.getInstance();
		DSTaskList list = createTaskList();
		fileHandler.saveToDataFile(list);
	}

	@Test
	public void testLoadFromDataFile() throws DSException, ParseException {
		testSaveToDataFile();
		
		DSDataFileHandler fileHandler = DSDataFileHandler.getInstance();
		DSTaskList listForSaving = createTaskList();
		DSTaskList  listForLoading = fileHandler.loadFromDataFile();
		
		assertTrue(listForLoading.size() == listForSaving.size());
		for(int i=0; i<listForLoading.size(); i++){
			assertTrue(listForLoading.get(i).compareTo(listForSaving.get(i)) == 0);
		}
	}
	
	//For Test
	public DSTaskList createTaskList() throws ParseException{
		DSTaskList list = new DSTaskList();
		for (int i = 0; i < 5; i++) {
			DSTask task = new DSTask();
			task.setTaskName("name" + i);
			task.setTaskStartTime(123 + i);
			task.setTaskEndTime(456 + i);
			list.add(task);
		}
		return list;
	}
}
